﻿using System;
using System.Collections.Generic;

using DotNetLib.Windows.Forms;

using ExtensibleEventViewer.Console.DomainModel;
using ExtensibleEventViewer.Core.Primitives;


namespace ExtensibleEventViewer.Console.Adapters
{
	internal class EventLogAdapter : AdapterBase<EventLogEntity>
	{
		public EventLogAdapter(EventLogEntity dataItem) : base(dataItem)
		{
		}

		protected override ContainerListViewItem CreateListViewItem(EventLogEntity dataItem)
		{
			ContainerListViewItem item = new ContainerListViewItem
			                             	{
			                             		Text = GetListViewItemText(dataItem, Settings),
			                             	};
			
			item.SetFilter(SubItemsFilter.Instance,  true);

			return item;
		}

		private static string GetListViewItemText(EventLogEntity dataItem, EventLogAdapterSettings settings)
		{
			if (settings.EnableRefresh && !settings.UseDefaultRefreshInterval)
			{
				return string.Format("{0} : {1} | Updated : {2} | Update interval : {3}", dataItem.ComputerName, dataItem.Name,
											  settings.LastUpdateTime == DateTime.MinValue ? "N/A" : settings.LastUpdateTime.ToString("HH:mm:ss"),
											  settings.RefreshInterval);
			}

			return string.Format("{0} : {1} | Updated : {2}", dataItem.ComputerName, dataItem.Name,
			                                  settings.LastUpdateTime == DateTime.MinValue ? "N/A" : settings.LastUpdateTime.ToString("HH:mm:ss"));
		}

		private EventLogAdapterSettings _settings;

		internal EventLogAdapterSettings Settings
		{
			get
			{
				if (_settings == null)
				{
					_settings = new EventLogAdapterSettings();

					_settings.RefreshInterval = AppSettings.Instance.RefreshTimeInterval;
					_settings.EnableRefresh = true;
					_settings.LastUpdateTime = DateTime.MinValue;
					_settings.UseDefaultRefreshInterval = true;
				}

				return _settings;
			}
			set
			{
				_settings = value;
			}
		}

		internal void RefreshRecords()
		{
			List<ContainerListViewItem> items = new List<ContainerListViewItem>(10);
			
			foreach (EventLogRecord record in DataItem.GetEventRecords())
			{
				EventLogRecordAdapter recordAdapter = new EventLogRecordAdapter(record);

				if (SubItemsFilter.Instance.Belongs(record))
				{
					items.Add(recordAdapter.ListViewItem);
				}
			}

			if(ListViewItem.ListView != null)
			{
				if (ListViewItem.ListView.InvokeRequired)
				{
					Action refreshDelegate = () => DoRefresh(items);

					ListViewItem.ListView.BeginInvoke(refreshDelegate);
				}
				else
				{
					DoRefresh(items);
				}
			}

		}

		private void DoRefresh(List<ContainerListViewItem> items)
		{
			bool isExpanded = ListViewItem.Expanded;

			ListViewItem.BeginUpdate();

			if (ListViewItem.HasChildren)
			{
				ListViewItem.Items.Clear();
			}

			items.Reverse();

			ListViewItem.Items.AddRange(items.ToArray());

			Settings.LastUpdateTime = DateTime.UtcNow;

			ListViewItem.Text = GetListViewItemText(DataItem, Settings);

			if (isExpanded)
			{
				ListViewItem.Expand();
			}

			ListViewItem.EndUpdate();
		}
	}
}